﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class ExamInfo : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        Exam ex = new Exam();
        ex.RestrictTime = 30;
        ex.TotalMark = 0;

        SqlTransaction transaction;

        //GET QUESTION CATEGORY GENERAL
        SqlCommand cmdQuestionGeneral = new SqlCommand("SELECT top 5 * FROM Question WHERE CategoryID = 1 ORDER BY NEWID()", sConfiguration.Configuration.Connection);
        SqlDataReader readerQuestionGeneral = cmdQuestionGeneral.ExecuteReader();

        //GET QUESTION CATEGORY MATH
        SqlCommand cmdQuestionMath = new SqlCommand("SELECT top 5 * FROM Question WHERE CategoryID = 2 ORDER BY NEWID()", sConfiguration.Configuration.Connection);
        SqlDataReader readerQuestionMath = cmdQuestionMath.ExecuteReader();

        //GET QUESTION CATEGORY COMPUTER
        SqlCommand cmdQuestionComputer = new SqlCommand("SELECT top 5 * FROM Question WHERE CategoryID = 3 ORDER BY NEWID()", sConfiguration.Configuration.Connection);
        SqlDataReader readerQuestionComputer = cmdQuestionComputer.ExecuteReader();

        transaction = sConfiguration.Configuration.Connection.BeginTransaction();

        try
        {
            SqlCommand cmdInsertExam = new SqlCommand("INSERT INTO Exam(RestrictTime, TotalMark) VALUES('" + ex.RestrictTime + "', '" + ex.TotalMark + "')", sConfiguration.Configuration.Connection);
            cmdInsertExam.ExecuteNonQuery();

            SqlCommand cmdExam = new SqlCommand("Select MAX(ID) Exam from Exam", sConfiguration.Configuration.Connection);
            int ExamID = (int)cmdExam.ExecuteScalar();
            
            double TotalMark = 0;


            //INSERT QUESTION CATEGORY GENERAL ON EXAM
            while (readerQuestionGeneral.Read())
            {
                string QuestionID = readerQuestionGeneral[0].ToString();
                TotalMark = TotalMark + double.Parse(readerQuestionGeneral[4].ToString());
                ExamQuestion eq = new ExamQuestion();
                eq.ExamID = ExamID;
                eq.QuestionID = int.Parse(QuestionID);
                new SqlCommand("INSERT INTO ExamQuestion(QuestionID, ExamID) VALUES('" + eq.QuestionID + "', '" + eq.ExamID + "')", sConfiguration.Configuration.Connection).ExecuteNonQuery();

            }

            //INSERT QUESTION CATEGORY MATH ON EXAM
            while (readerQuestionMath.Read())
            {
                string QuestionID = readerQuestionMath[0].ToString();
                TotalMark = TotalMark + double.Parse(readerQuestionMath[4].ToString());
                ExamQuestion eq = new ExamQuestion();
                eq.ExamID = ExamID;
                eq.QuestionID = int.Parse(QuestionID);
                new SqlCommand("INSERT INTO ExamQuestion(QuestionID, ExamID) VALUES('" + eq.QuestionID + "', '" + eq.ExamID + "')", sConfiguration.Configuration.Connection).ExecuteNonQuery();

            }

            //INSERT QUESTION CATEGORY COMPUTER ON EXAM
            while (readerQuestionComputer.Read())
            {
                string QuestionID = readerQuestionComputer[0].ToString();
                TotalMark = TotalMark + double.Parse(readerQuestionComputer[4].ToString());
                ExamQuestion eq = new ExamQuestion();
                eq.ExamID = ExamID;
                eq.QuestionID = int.Parse(QuestionID);
                new SqlCommand("INSERT INTO ExamQuestion(QuestionID, ExamID) VALUES('" + eq.QuestionID + "', '" + eq.ExamID + "')", sConfiguration.Configuration.Connection).ExecuteNonQuery();

            }

            //UPDATE TOTAL MARK
            SqlCommand cmdUpdateExam = new SqlCommand("UPDATE Exam SET TotalMark = " + TotalMark + " WHERE ID = " + ExamID, sConfiguration.Configuration.Connection);
            cmdUpdateExam.ExecuteNonQuery();

            Session.Add("Exam", ExamID);

            transaction.Commit();
        }
        catch (SqlException sqlError)
        {
            transaction.Rollback();
            MessagesBox.Show("Error : " + sqlError.Message);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        /*if (Session["Exam"] != null)
        {
            Response.Redirect("Test.aspx");
        }*/

        if (Session["Username"] == null)
        {
            Response.Redirect("Default.aspx");
        }
        else
        {
            txtFullname.Text = Session["Fullname"].ToString();
            txtEmail.Text = Session["Email"].ToString();
            txtPhone.Text = Session["Mobile"].ToString();
            txtAddress.Text = Session["Address"].ToString();
        }
        
    }

    protected void btnStart_Click(object sender, EventArgs e)
    {
        Response.Redirect("Step1.aspx");
    }
}